<?php

namespace App\Http\Controllers\Api;

use App\Datalog\Events\PayEventLog;
use App\Datalog\Events\RoleLoginEventLog;
use App\Datalog\Events\RoleRegisterEventLog;
use App\Http\Controllers\Controller;
use App\Models\Log\LogUserRoleLogin;
use App\Models\Orders;
use App\Models\UserRoles;
use App\Models\Users;
use App\Services\AdPostbackService;
use App\Services\GameHtService;
use App\Services\SdkService;
use App\Services\UserRoleService;
use App\Services\UserRoleUniqueIdService;
use Illuminate\Http\Request;
use YQ\YqExtend;

class UserRoleController extends Controller
{
    public function reg(Request $request)
    {
        $params = $request->all();

        // 校验签名
        $ret = SdkService::getInstance()->checkSign($params);
        if ($ret === false) {
            return yqerr(101);
        }

        $game_id = $params['game_id'];
        $channel = $params['channel'];
        $user_id = $params['user_id'];
        $role_id = $params['role_id'];
        $server_id = toUnsignedInt(($params['server_id'] ?? 0));

        $unique_id = UserRoleUniqueIdService::getInstance()->getOrCreateUniqueId($user_id, $server_id, $role_id);
        $role = UserRoleService::getInstance()->getOneByUniqueId($unique_id);
        if (!$role) {
            $role                  = new UserRoles;
            $role->create_time     = time();
            $role->game_id         = $game_id;
            $role->channel         = $channel;
            $role->user_id         = $user_id;
            $role->role_id         = $role_id;
            $role->ip              = YqExtend::getIP();
            $role->role_name       = $params['role_name'] ?? '';
            $role->server_id       = $server_id;
            $role->server_name     = $params['server_name'] ?? '';
            $role->role_level      = 1;
            $role->last_login_time = time();
            $role->role_unique_id  = $unique_id;
            $role->save();

            RoleRegisterEventLog::getInstance()->addLog($role);
        }

        return yqok();
    }

    public function login(Request $request)
    {
        $params = $request->all();

        // 校验签名
        $ret = SdkService::getInstance()->checkSign($params);
        if ($ret === false) {
            return yqerr(101);
        }

        $user_id = $params['user_id'];
        $role_id = $params['role_id'];
        $role_level = toUnsignedInt(($params['role_level'] ?? 1));
        $server_id  = toUnsignedInt(($params['server_id'] ?? 0));
        $role_name = $params['role_name'] ?? '';

        $unique_id = UserRoleUniqueIdService::getInstance()->getOrCreateUniqueId($user_id, $server_id, $role_id);
        $role = UserRoleService::getInstance()->getOneByUniqueId($unique_id);
        // 如果没有找到角色，则创建一条
        // 如果等级等于1则添加创建时间
        if (!$role && $role_level > 1) {
            $role                  = new UserRoles;
            $role->create_time     = time();
            $role->game_id         = $params['game_id'];
            $role->channel         = $params['channel'];
            $role->user_id         = $user_id;
            $role->role_id         = $role_id;
            $role->ip              = YqExtend::getIP();
            $role->role_name       = $role_name;
            $role->server_id       = $server_id;
            $role->server_name     = $params['server_name'] ?? '';
            $role->last_login_time = time();
            $role->role_level      = $role_level;
            $role->role_unique_id  = $unique_id;
            $role->save();

            RoleRegisterEventLog::getInstance()->addLog($role);
        }

        if ($role) {
            $role->role_level = $role_level;
            $role->last_login_time = time();
            if (!empty($role_name) && $role->role_name != $role_name) {
                $role->role_name = $role_name;
            }
            $role->save();
        }

        $log                 = new LogUserRoleLogin;
        $log->create_time    = time();
        $log->game_id        = $params['game_id'];
        $log->channel        = $params['channel'];
        $log->user_id        = $user_id;
        $log->role_id        = $role_id;
        $log->role_name      = $params['role_name'] ?? '';
        $log->server_id      = $server_id;
        $log->server_name    = $params['server_name'] ?? '';
        $log->role_level     = $role_level;
        $log->role_unique_id = $unique_id;
        $log->ip             = YqExtend::getIP();
        $log->save();

        RoleLoginEventLog::getInstance()->addLog($log);

        return yqok();
    }

    public function levelUp(Request $request)
    {
        $params = $request->all();

        // 校验签名
        $ret = SdkService::getInstance()->checkSign($params);
        if ($ret === false) {
            return yqerr(101);
        }

        $user_id = $params['user_id'];
        $role_id = $params['role_id'];
        $server_id  = toUnsignedInt(($params['server_id'] ?? 0));
        $role_level = toUnsignedInt(($params['role_level'] ?? 1));
        $role_name = $params['role_name'] ?? '';

        $role = UserRoleService::getInstance()->getOne($user_id, $server_id, $role_id);
        if (!$role) {
            return yqerr(6053);
        }

        $role->role_level = $role_level;
        if (!empty($role_name) && $role->role_name != $role_name) {
            $role->role_name = $role_name;
        }

        $role->save();

        return yqok();
    }

    public function xuniPay(Request $request)
    {
        $params = $request->input();

        $user_id = $params['user_id'];
        $user = Users::find($user_id);
        if (!$user) {
            return yqerr(6050);
        }

        $game_trade_no = $params['game_trade_no'];
        $order = Orders::whereRaw("user_id={$user_id} AND game_trade_no='{$game_trade_no}'")->first();
        if ($order) {
            return yqerr(6051);
        }

        $server_id = $params['server_id'];
        $role_id = $params['role_id'];
        $unique_id = UserRoleUniqueIdService::getInstance()->getOrCreateUniqueId($user_id, $server_id, $role_id);

        $order                  = new Orders;
        $order->trade_no        = YqExtend::uniqid();
        $order->game_trade_no   = $params['game_trade_no'];
        $order->amount          = $params['amount'];
        $order->money           = round($params['amount']/100, 2);
        $order->user_id         = $user_id;
        $order->game_id         = $user->game_id;
        $order->channel         = $user->channel;
        $order->game_channel_id = $user->game_channel_id;
        $order->channel_openid  = $user->channel_openid;
        $order->goods_id        = $params['goods_id'];
        $order->goods_name      = $params['goods_name'];
        $order->goods_type      = Orders::GOODS_TYPE_XUNI;
        $order->server_id       = $params['server_id'];
        $order->server_name     = $params['server_name'];
        $order->role_id         = $params['role_id'];
        $order->role_name       = $params['role_name'];
        $order->role_level      = $params['role_level'];
        $order->role_unique_id  = $unique_id;
        $order->create_time     = time();
        $order->status          = Orders::PAY_STATUS_XUNI;
        $order->ip              = YqExtend::getIp();
        $order->save();

        // 数据落地
        PayEventLog::getInstance()->addLog($order);

        // 进行一次买量回传
        AdPostbackService::getInstance()->postbackPay($order);

        return yqok();
    }

    public function event(Request $request)
    {
        return yqok();
    }
}
